/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import tcnplanner.Account;
import tcnplanner.Main;
import tcnplanner.Test.TestType;
import org.junit.Ignore;
import java.util.logging.Logger;
import tcnplanner.TestSetup;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.logging.Level;
import tcnplanner.Reservation;
import tcnplanner.Controller;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;


/**
 *
 * @author George Rijk
 */
public class testController {

    public testController() {
    }

    private Controller ctrl;

    // Let's make logging to the IDE output a bit nicer.
    private static final Logger log = Logger.getLogger("tcnControllerTest");

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
        ctrl = new Controller(); // setup clean controller object for every test.
    }

    @After
    public void tearDown() {
    }


    @Test
    public void addReservation() {
        // prepare data for new reservation:
        Account cust1 = new Account("Henk25", "KoeMetWeide", Controller.ActorType.EXTERNCUSTOMER, "Henk Daalder", "Den Dolech 14", "040-28736618", "h.daalder@edutel.nl");
        tcnplanner.Test test = new tcnplanner.Test(TestType.WAFERBATCH, "SEC871v5 Wafer x25", 25);
        TestSetup testSetup = new TestSetup();
        Date reservationDate = new Date(); // let's reserve stuff at the default date for this test.

        // create the actual reservation
        Reservation r1 = new Reservation(reservationDate, testSetup, test, cust1);

        // test controller method operational behaviour.
        log.log(Level.INFO, "Testing addition of a single reservation.");
        ctrl.addReservation(r1);
        Reservation result = ctrl.findReservation(r1.getId());

        if(result == null) { // spit out some explanation on what happened :-)
            log.log(Level.WARNING, "Retrieving reservation from controller with id = {0} returned null-pointer.", r1.getId());
        }

        assertNotNull(result);
    }

    @Test
    public void editReservation() {
        // prepare data for new reservation:
        Account cust1 = new Account("Henk25", "KoeMetWeide", Controller.ActorType.EXTERNCUSTOMER, "Henk Daalder", "Den Dolech 14", "040-28736618", "h.daalder@edutel.nl");
        tcnplanner.Test test = new tcnplanner.Test(TestType.WAFERBATCH, "SEC871v5 Wafer x25", 25);
        TestSetup testSetup = new TestSetup();
        Date reservationDate = new Date(); // let's reserve stuff at the default date for this test.
        Date reservationDate2 = new Date(2011, 9, 5);

        // create the actual reservation
        Reservation r1 = new Reservation(reservationDate, null, testSetup, test, cust1, "is een pinda!");

        // test controller method operational behaviour.
        log.log(Level.INFO, "Preparing reservation 1 with id = {0}, comment = {1}", new Object[]{r1.getId(), r1.getComment()});
        ctrl.addReservation(r1);
        Reservation r2 = ctrl.findReservation(r1.getId()); // makes sure we are testing on the same reservation ID
        r2.setComment("Slaat zijn vrouw!"); // edit copy of r1 with some randomness :-)
        ctrl.editReservation(r2); // go go edit!
        log.log(Level.INFO, "Editting reservation with id = {0}, comment = {1}", new Object[]{r2.getId(), r2.getComment()});

        // verify results:

        // did the edit work at all? ...
        Reservation result = ctrl.findReservation(r1.getId());
        assertTrue(result.getComment().equals("Slaat zijn vrouw!"));
    }

    @Test
    public void findReservationTest() {
        /* ***** NOTE ***** */
        // A new Controller instance is created before this test (setUp method).
        // A new reservation service is created indise of the creation of a new Controller instance.
        // Conclusion: we have 0 reservations right now.

        // test on non-existent reservation
        log.log(Level.INFO, "Testing findReservation on empty list. Expected: null");
        Reservation result = ctrl.findReservation(0); // id == 0 is the default first value for a new reservation.
        assertNull(result);
        if(result == null) {
            log.log(Level.INFO, "RESULT: Testing findReservation on empty list. Got: null");
        } else {
            log.log(Level.INFO, "RESULT: Testing findReservation on empty list. Got: {0}", result.toString());
        }

        // test on negative reservation id.
        // prepare data for new reservation:
        /* *** NOTE *** */
        // 2011-06-25: This test is no longer possible, because the constructor with a reservation nr. argument was removed. Therefore we cannot force a reservation ID anymore.
        //  The functionality is now guaranteed to be correct by the JPA implementation in the database/persistence layer.
        
//        Account cust1 = new Account("Henk25", "KoeMetWeide", Controller.ActorType.EXTERNCUSTOMER, "Henk Daalder", "Den Dolech 14", "040-28736618", "h.daalder@edutel.nl");
//        tcnplanner.Test test = new tcnplanner.Test(TestType.WAFERBATCH, "SEC871v5 Wafer x25", 25);
//        TestSetup testSetup = new TestSetup();
//        Date reservationDate = new Date(); // let's reserve stuff at the default date for this test.
//
//        // create the actual reservation
//        log.log(Level.INFO, "Testing findReservation on negative ID (negative ID forced, just testing the finding). Expected: Allowed");
//        Reservation r1 = new Reservation(-1, reservationDate, null, testSetup, test, cust1, "is een pinda!");
//        ctrl.addReservation(r1);
//        result = ctrl.findReservation(-1);
//
//        boolean condition = result.equals(r1);
//        if(condition) {
//            log.log(Level.INFO, "RESULT: Testing findReservation on negative ID. Got: Allowed");
//        } else {
//            log.log(Level.INFO, "RESULT: Testing findReservation on negative ID. Got: Denied");
//        }
//        assertTrue(condition);
    }


    // ---- Accounting tests ----
    
        // Test on registering a new account successfully
    @Test void registerAccountTest() {
        log.log(Level.INFO, "Testing registerAccount for successfull (allowable) registration.");
        boolean success = ctrl.RegisterAccount("h.aapjes", "banaan", Controller.ActorType.OPERATOR, "Haas Aapjes", "Konijnenhol 13", "0499-387265387", "aapjes@konijnenhol-europa.nl");
        assertTrue(success);
        
        if(success) {
            log.log(Level.INFO, "RESULT: Account was successfully registered.");
        } else {
            log.log(Level.INFO, "RESULT: Account failed to be registered.");
        }
    }
}